package com.damon.day03;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.io.IOUtils;

import java.io.*;

/**
 * @Date 2020/4/17 21:27
 * @Created by Damon
 */
public class QueryBpmnFile {
    public static void main(String[] args) throws IOException {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        /*
        * 从数据库的act_ge_bytearray表中读取两个资源文件
        * 将两个资源文件保存到路径: d:\bpmn
        * */
        RepositoryService repositoryService = processEngine.getRepositoryService();

        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();

        processDefinitionQuery.processDefinitionKey("holiday");

        ProcessDefinition processDefinition = processDefinitionQuery.singleResult();

        String deploymentId = processDefinition.getDeploymentId();

        //deploymentId: 部署id    processDefinition.getDiagramResourceName(): 资源名称
        //processDefinition.getDiagramResourceName() 代表获取png图片资源的名称
        InputStream pngIs = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName());

        //processDefinition.getResourceName() 代表获取bpmn文件的名称
        InputStream bpmnIs = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());

        OutputStream pngOs = new FileOutputStream("d:\\bpmn\\"+ processDefinition.getDiagramResourceName());
        OutputStream bpmnOs = new FileOutputStream("d:\\bpmn\\"+ processDefinition.getResourceName());

        IOUtils.copy(pngIs, pngOs);
        IOUtils.copy(bpmnIs, bpmnOs);

        pngIs.close();
        bpmnIs.close();
        pngOs.close();
        bpmnOs.close();

    }
}
